package com.zxy.supplier_system.server.service

import com.zxy.supplier_system.server.configuration.EmployeeOperateRecordProperties
import com.zxy.supplier_system.server.controller.EmployeeOperateRecordController
import com.zxy.supplier_system.server.entity.*
import com.zxy.supplier_system.server.repository.EmployeeOperateRecordRepository
import com.zxy.supplier_system.server.repository.SystemServiceRepository
import com.zxy.supplier_system.server.utils.joinOnce
import com.zxy.supplier_system.server.utils.toIntIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.domain.Specification
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.server.ResponseStatusException
import java.time.OffsetDateTime

@Service
class EmployeeOperateRecordService(
    private val employeeOperateRecordRepository: EmployeeOperateRecordRepository,
    private val employeeOperateRecordProperties: EmployeeOperateRecordProperties,
) {

    @Transactional
    fun clearExpiredRecord():Int {
        return employeeOperateRecordRepository.deleteAllByCreatedDateTimeBefore(OffsetDateTime.now().minusDays(employeeOperateRecordProperties.expiredDays.toLong()))
    }

    @Transactional
    fun createEmployee(employee: Employee) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                listOf(
                    employeeName(),
                    create("人员"),
                    highlightText(employee.name),
                )
            )
        )
    }

    @Transactional
    fun updateEmployee(employee: Employee) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                listOf(
                    employeeName(),
                    update("人员"),
                    highlightText(employee.name),
                )
            )
        )
    }

    @Transactional
    fun enableEmployee(employee: Employee) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                listOf(
                    employeeName(),
                    basicOperate("启用", "人员"),
                    highlightText(employee.name),
                )
            )
        )
    }

    @Transactional
    fun disableEmployee(employee: Employee) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                listOf(
                    employeeName(),
                    basicOperate("禁用", "人员"),
                    highlightText(employee.name),
                )
            )
        )
    }

    private fun employeeName(): EmployeeOperateRecord.ContentItem {
        val employee =
            EmployeeService.getCurrentContextEmployee() ?: throw ResponseStatusException(HttpStatus.UNAUTHORIZED)
        return EmployeeOperateRecord.ContentItem(employee.name, highlight = true)
    }

    private fun text(text: String): EmployeeOperateRecord.ContentItem {
        return EmployeeOperateRecord.ContentItem(text)
    }

    private fun highlightText(text: String): EmployeeOperateRecord.ContentItem {
        return EmployeeOperateRecord.ContentItem(text, highlight = true)
    }

    private fun update(resource: String): EmployeeOperateRecord.ContentItem {
        return basicOperate("修改", resource)
    }

    private fun create(resource: String): EmployeeOperateRecord.ContentItem {
        return basicOperate("新建", resource)
    }

    private fun basicOperate(operate: String, resource: String): EmployeeOperateRecord.ContentItem {
        return text("${operate}了$resource：")
    }

    fun queryEmployeeOperateRecords(
        request: EmployeeOperateRecordController.QueryEmployeeOperateRecordRequest,
        pageable: Pageable
    ): Page<EmployeeOperateRecordController.QueryEmployeeOperateRecordResponse> {
        return this.employeeOperateRecordRepository.findAll(
            Specification.allOf(
                EmployeeOperateRecordRepository.Specifications.createdDateTimeLessThanOrEqualTo(
                    request.createdEndDate,
                ),
                EmployeeOperateRecordRepository.Specifications.createdDateTimeGreaterThanOrEqualTo(
                    request.createdStartDate,
                ),
                SystemServiceRepository.Specifications.systemServiceEquals(
                    { root ->
                        root.joinOnce<EmployeeOperateRecord, Employee>(
                            "employee"
                        )
                    },
                    EmployeeService.getCurrentContextEmployee()?.systemService ?: throw ResponseStatusException(
                        HttpStatus.UNAUTHORIZED
                    )
                ),
                EmployeeOperateRecordRepository.Specifications.operatorIn(request.operatorIds),
                EmployeeOperateRecordRepository.Specifications.orderByCreatedDateTime(),
            ),
            pageable
        ).map {
            EmployeeOperateRecordController.QueryEmployeeOperateRecordResponse(
                it.content,
                it.id,
                it.createdDateTime,
                it.employee.toIntIdAndName(),
            )
        }
    }

    @Transactional
    fun updateFactory(data: Factory) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                update("厂商"),
                highlightText(data.name)
            )
        )
    }

    @Transactional
    fun createFactory(factory: Factory) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                create("厂商"),
                highlightText(factory.name)
            )
        )
    }

    @Transactional
    fun updateGoodsBrandGroup(data: GoodsBrandGroup) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                update("品牌组"),
                highlightText(data.name)
            )
        )
    }


    @Transactional
    fun createGoodsBrandGroup(goodsBrandGroup: GoodsBrandGroup) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                create("品牌组"),
                highlightText(goodsBrandGroup.name)
            )
        )
    }

    fun updateGoodsBrand(data: GoodsBrand) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                update("品牌"),
                highlightText(data.name)
            )
        )
    }

    fun createGoodsBrand(data: GoodsBrand) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                create("品牌"),
                highlightText(data.name)
            )
        )
    }

    fun enableGoodsBrand(goodsBrand: GoodsBrand) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                basicOperate("启用", "品牌"),
                highlightText(goodsBrand.name),
            )
        )
    }

    fun disableGoodsBrand(goodsBrand: GoodsBrand) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                basicOperate("禁用", "品牌"),
                highlightText(goodsBrand.name),
            )
        )
    }

    fun enableFactory(factory: Factory) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                basicOperate("启用", "厂商"),
                highlightText(factory.name),
            )
        )
    }

    fun disableFactory(factory: Factory) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                basicOperate("禁用", "厂商"),
                highlightText(factory.name),
            )
        )
    }

    fun updateGoodsCategory(data: GoodsCategory) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                update("货品分类"),
                highlightText(data.name)
            )
        )
    }

    fun createGoodsCategory(data: GoodsCategory) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                create("货品分类"),
                highlightText(data.name)
            )
        )
    }

    fun addGoods(goods: Goods) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                create("商品"),
                highlightText(goods.name),
                text("   条码："),
                highlightText(goods.barcode),
            )
        )
    }

    fun updateGoods(goods: Goods) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                update("商品"),
                highlightText(goods.name),
                text("   条码："),
                highlightText(goods.barcode),
            )
        )
    }

    fun disableGoods(goods: Goods) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                basicOperate("禁用", "商品"),
                highlightText(goods.name),
                text("   条码："),
                highlightText(goods.barcode),
            )
        )
    }

    fun enableGoods(goods: Goods) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                basicOperate("启用", "商品"),
                highlightText(goods.name),
                text("   条码："),
                highlightText(goods.barcode),
            )
        )
    }

    fun batchEnableOrDisableGoods(goodsList:List<Goods>,enable: Boolean){
        if (goodsList.isEmpty()) { return }
        val suffix = if(goodsList.size>1) "批量" else ""
        val action = if (enable) "启用" else "禁用"
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                basicOperate("${suffix}$action", "商品"),
                *goodsList.flatMapIndexed {index,goods->
                    ArrayList<EmployeeOperateRecord.ContentItem>(2).apply {
                        if (index!=0){
                            this.add(text(","))
                        }
                        add(EmployeeOperateRecord.ContentItem(
                            goods.name,
                            resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                                EmployeeOperateRecord.ContentItem.ResourceType.GOODS,
                                goods.id.toString(),
                            ),
                        ),)
                    }
                }.toTypedArray(),
            )
        )
    }

    fun importFromExcelItems(size: Int) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("导入了"),
                highlightText(size.toString()),
                text("个商品"),
            )
        )
    }

    fun addMarketDeliveryOrderItem(marketDeliveryOrder: MarketDeliveryOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("添加了"),
                highlightText(marketDeliveryOrder.goodsList.last().goods.name),
                text("到"),
                highlightText(marketDeliveryOrder.market.name),
                EmployeeOperateRecord.ContentItem(
                    "出货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketDeliveryOrder,
                        marketDeliveryOrder.id.toString()
                    ),
                ),
                text("中"),
            )
        )
    }

    fun deleteMarketDeliveryOrderItem(marketDeliveryOrderItem: MarketDeliveryOrderItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("从"),
                highlightText(marketDeliveryOrderItem.order.market.name),
                EmployeeOperateRecord.ContentItem(
                    "出货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketDeliveryOrder,
                        marketDeliveryOrderItem.order.id.toString()
                    ),
                ),
                text("中删除了"),
                highlightText(marketDeliveryOrderItem.goods.name),
            )
        )
    }

    fun updateMarketDeliveryItemAmount(marketDeliveryOrderItem: MarketDeliveryOrderItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("调整"),
                highlightText(marketDeliveryOrderItem.order.market.name),
                EmployeeOperateRecord.ContentItem(
                    "出货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketDeliveryOrder,
                        marketDeliveryOrderItem.order.id.toString()
                    ),
                ),
                text("中商品"),
                highlightText(marketDeliveryOrderItem.goods.name),
                text("数量为"),
                highlightText(marketDeliveryOrderItem.amount.toString()),
            )
        )
    }

    fun validateMarketDeliveryOrder(marketDeliveryOrder: MarketDeliveryOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("验收了"),
                highlightText(marketDeliveryOrder.market.name),
                EmployeeOperateRecord.ContentItem(
                    "出货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketDeliveryOrder,
                        marketDeliveryOrder.id.toString()
                    ),
                ),
            )
        )
    }

    fun auditMarketDeliveryOrder(marketDeliveryOrder: MarketDeliveryOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("审核了"),
                highlightText(marketDeliveryOrder.market.name),
                EmployeeOperateRecord.ContentItem(
                    "出货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketDeliveryOrder,
                        marketDeliveryOrder.id.toString()
                    ),
                ),
            )
        )
    }

    fun createMarketOrder(marketOrder: MarketOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("创建了"),
                highlightText(marketOrder.market.name),
                EmployeeOperateRecord.ContentItem(
                    "订单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketOrder,
                        marketOrder.id.toString()
                    ),
                ),
            )
        )
    }

    fun addMarketOrderItem(marketOrder: MarketOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("添加了"),
                highlightText(marketOrder.goodsList.last().goods.name),
                text("到"),
                highlightText(marketOrder.market.name),
                EmployeeOperateRecord.ContentItem(
                    "订单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketOrder,
                        marketOrder.id.toString()
                    ),
                ),
                text("中"),
            )
        )
    }

    fun updateMarketOrderItemAmount(marketOrderItem: MarketOrderItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("调整"),
                highlightText(marketOrderItem.marketOrder.market.name),
                EmployeeOperateRecord.ContentItem(
                    "订单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketOrder,
                        marketOrderItem.marketOrder.id.toString()
                    ),
                ),
                text("中商品"),
                highlightText(marketOrderItem.subGoods?.name ?: marketOrderItem.goods.name),
                text("数量为"),
                highlightText(marketOrderItem.amount.toString()),
            )
        )
    }

    fun deliveryMarketOrder(marketOrder: MarketOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("通过"),
                highlightText(marketOrder.market.name),
                EmployeeOperateRecord.ContentItem(
                    "订单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketOrder,
                        marketOrder.id.toString()
                    ),
                ),
                text("开了"),
                EmployeeOperateRecord.ContentItem(
                    "送货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketDeliveryOrder,
                        marketOrder.id.toString()
                    ),
                ),
            )
        )
    }

    fun submitMarketOrder(marketOrder: MarketOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("提交了"),
                highlightText(marketOrder.market.name),
                EmployeeOperateRecord.ContentItem(
                    "订单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketOrder,
                        marketOrder.id.toString()
                    ),
                ),
            )
        )
    }

    fun updateMarket(market: Market) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                update("卖场"),
                highlightText(market.name)
            )
        )
    }

    fun createMarket(market: Market) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                create("卖场"),
                highlightText(market.name)
            )
        )
    }

    fun createCheckInventoryRecord(checkInventoryRecord: CheckInventoryRecord) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                create("创建了盘点"),
                EmployeeOperateRecord.ContentItem.withResource(
                    checkInventoryRecord.name,
                    EmployeeOperateRecord.ContentItem.ResourceType.CheckInventoryRecord,
                    checkInventoryRecord.id.toString(),
                ),
            )
        )
    }

    fun addCheckInventoryRecordItem(checkInventoryRecord: CheckInventoryRecord) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("在盘点"),
                EmployeeOperateRecord.ContentItem.withResource(
                    "“${checkInventoryRecord.name}”",
                    EmployeeOperateRecord.ContentItem.ResourceType.CheckInventoryRecord,
                    checkInventoryRecord.id.toString(),
                ),
                text("中添加了商品"),
                highlightText(checkInventoryRecord.items.last().goods.name),
                text("到"),
            )
        )
    }

    fun updateCheckInventoryRecordItemAmount(checkInventoryRecordItem: CheckInventoryRecordItem) {
        val checkInventoryRecord = checkInventoryRecordItem.record
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("调整盘点"),
                EmployeeOperateRecord.ContentItem.withResource(
                    "“${checkInventoryRecord.name}”",
                    EmployeeOperateRecord.ContentItem.ResourceType.CheckInventoryRecord,
                    checkInventoryRecord.id.toString(),
                ),
                highlightText(checkInventoryRecordItem.record.name),
                text("中商品"),
                highlightText(if (checkInventoryRecordItem.subGoods == null) checkInventoryRecordItem.goods.name else checkInventoryRecordItem.subGoods!!.name + "（${checkInventoryRecordItem.goods.name}）"),
                text("数量为"),
                highlightText(checkInventoryRecordItem.amount.toString()),
            )
        )
    }

    fun submitCheckInventoryRecord(checkInventoryRecord: CheckInventoryRecord) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("提交了盘点："),
                EmployeeOperateRecord.ContentItem.withResource(
                    "“${checkInventoryRecord.name}”",
                    EmployeeOperateRecord.ContentItem.ResourceType.CheckInventoryRecord,
                    checkInventoryRecord.id.toString(),
                ),
            ),
        )
    }

    fun createDirectSaleClient(directSaleClient: DirectSaleClient) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                listOf(
                    employeeName(),
                    create("经销客户"),
                    highlightText(directSaleClient.name),
                )
            )
        )
    }

    fun updateDirectSaleClient(directSaleClient: DirectSaleClient) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                listOf(
                    employeeName(),
                    update("经销客户"),
                    highlightText(directSaleClient.name),
                )
            )
        )
    }

    fun createDirectSaleOrder(directSaleOrder: DirectSaleOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("创建了"),
                highlightText(directSaleOrder.client.name),
                EmployeeOperateRecord.ContentItem(
                    "采购单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.DirectSaleOrder,
                        directSaleOrder.id.toString()
                    ),
                ),
            )
        )
    }

    fun addDirectSaleOrderItem(directSaleOrder: DirectSaleOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("添加了"),
                highlightText(directSaleOrder.items.last().goods.name),
                text("到"),
                highlightText(directSaleOrder.client.name),
                EmployeeOperateRecord.ContentItem(
                    "经销单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.DirectSaleOrder,
                        directSaleOrder.id.toString()
                    ),
                ),
                text("中"),
            )
        )
    }

    fun submitDirectSaleOrder(directSaleOrder: DirectSaleOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("提交了"),
                highlightText(directSaleOrder.client.name),
                EmployeeOperateRecord.ContentItem(
                    "经销单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketOrder,
                        directSaleOrder.id.toString()
                    ),
                ),
            )
        )
    }

    fun updateDirectSaleOrderItemAmount(directSaleOrderItem: DirectSaleOrderItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("调整"),
                highlightText(directSaleOrderItem.order.client.name),
                EmployeeOperateRecord.ContentItem(
                    "经销单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketOrder,
                        directSaleOrderItem.order.id.toString()
                    ),
                ),
                text("中商品"),
                highlightText(directSaleOrderItem.goods.name),
                text("数量为"),
                highlightText(directSaleOrderItem.amount.toString()),
            )
        )
    }

    fun updateProcurementSupplier(procurementSupplier: ProcurementSupplier) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                update("供货商"),
                highlightText(procurementSupplier.name)
            )
        )
    }

    fun createProcurementSupplier(procurementSupplier: ProcurementSupplier) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                create("供货商"),
                highlightText(procurementSupplier.name)
            )
        )
    }

    fun createProcurement(procurement: Procurement) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("创建了"),
                highlightText(procurement.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.Procurement,
                        procurement.id.toString()
                    ),
                ),
            )
        )
    }

    fun updateProcurement(procurement: Procurement) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("修改了"),
                highlightText(procurement.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.Procurement,
                        procurement.id.toString()
                    ),
                ),
            )
        )
    }

    fun addProcurementItem(marketOrder: Procurement) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("添加了"),
                highlightText(marketOrder.items.last().goods.name),
                text("到"),
                highlightText(marketOrder.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.Procurement,
                        marketOrder.id.toString()
                    ),
                ),
                text("中"),
            )
        )
    }

    fun updateProcurementItemAmount(procurementItem: ProcurementItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("调整"),
                highlightText(procurementItem.procurement.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.Procurement,
                        procurementItem.procurement.id.toString()
                    ),
                ),
                text("中商品"),
                highlightText(procurementItem.subGoods?.name?:procurementItem.goods.name),
                text("数量为"),
                highlightText(procurementItem.amount.toString()),
            )
        )
    }

    fun updateProcurementItemPurchasePrice(procurementItem: ProcurementItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("调整"),
                highlightText(procurementItem.procurement.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.Procurement,
                        procurementItem.procurement.id.toString()
                    ),
                ),
                text("中商品"),
                highlightText(procurementItem.subGoods?.name?:procurementItem.goods.name),
                text("采购价为"),
                highlightText(procurementItem.purchasePrice.toString()),
            )
        )
    }

    @Transactional
    fun updateProcurementItemNote(item: ProcurementItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("备注了"),
                highlightText(item.procurement.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.Procurement,
                        item.procurement.id.toString()
                    ),
                ),
                text("中商品"),
                highlightText(item.subGoods?.name ?: item.goods.name),
                text("："),
                highlightText(item.note?:"<空>"),
            )
        )
    }

    fun submitProcurement(procurement: Procurement) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("提交了"),
                highlightText(procurement.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.Procurement,
                        procurement.id.toString()
                    ),
                ),
            )
        )
    }

    fun addProcurementReceiptItem(procurementReceipt: ProcurementReceipt) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("添加了"),
                highlightText(procurementReceipt.items.last().goods.name),
                text("到"),
                highlightText(procurementReceipt.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购入库单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.PROCUREMENT_RECEIPT,
                        procurementReceipt.id.toString()
                    ),
                ),
                text("中"),
            )
        )
    }

    fun validateProcurementReceipt(procurementReceipt: ProcurementReceipt) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("验证了"),
                highlightText(procurementReceipt.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购入库单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.PROCUREMENT_RECEIPT,
                        procurementReceipt.id.toString()
                    ),
                ),
            )
        )
    }

    fun deleteProcurementReceiptItem(procurementReceiptItem: ProcurementReceiptItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("从"),
                highlightText(procurementReceiptItem.procurementReceipt.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购入库单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.PROCUREMENT_RECEIPT,
                        procurementReceiptItem.procurementReceipt.id.toString()
                    ),
                ),
                text("中"),
                text("删除了"),
                highlightText(procurementReceiptItem.goods.name),
            )
        )
    }

    fun validateProcurementReceiptItem(procurementReceiptItem: ProcurementReceiptItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("验收了"),
                highlightText(procurementReceiptItem.procurementReceipt.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购入库单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.PROCUREMENT_RECEIPT,
                        procurementReceiptItem.procurementReceipt.id.toString()
                    ),
                ),
                text("中的"),
                highlightText(procurementReceiptItem.practicalAmount.toString()),
                highlightText(procurementReceiptItem.goods.basicUnit?.name?:""),
                highlightText(procurementReceiptItem.goods.name),
            )
        )
    }

    fun updateProcurementReceiptItem(procurementReceiptItem: ProcurementReceiptItem) {
        val contentList = mutableListOf(
            employeeName(),
            text("调整"),
            highlightText(procurementReceiptItem.procurementReceipt.supplier.name),
            EmployeeOperateRecord.ContentItem(
                "采购入库单",
                resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                    EmployeeOperateRecord.ContentItem.ResourceType.PROCUREMENT_RECEIPT,
                    procurementReceiptItem.procurementReceipt.id.toString()
                ),
            ),
            text("中商品"),
            highlightText(procurementReceiptItem.goods.name),

        )
        if (procurementReceiptItem.subGoods!=null){
            contentList.add(
                highlightText("("+procurementReceiptItem.subGoods!!.name+")",)
            )
        }
        contentList.addAll(listOf(text("数量为"),
            highlightText(procurementReceiptItem.amount.toString()),))
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                *contentList.toTypedArray()
            )
        )
    }

    fun receipt(procurement: Procurement) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("入库了"),
                highlightText(procurement.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.Procurement,
                        procurement.id.toString()
                    ),
                ),
            )
        )
    }

    fun createMarketCheckInventoryRecord(marketCheckInventoryRecord: MarketCheckInventoryRecord) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("创建了"),
                highlightText(marketCheckInventoryRecord.market.name),
                EmployeeOperateRecord.ContentItem(
                    "盘点单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MARKET_CHECK_INVENTORY_RECORD,
                        marketCheckInventoryRecord.id.toString()
                    ),
                ),
            )
        )
    }

    fun addMarketCheckInventoryRecordItem(marketCheckInventoryRecord: MarketCheckInventoryRecord) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("添加了"),
                highlightText(marketCheckInventoryRecord.items.last().goods.name),
                text("到"),
                highlightText(marketCheckInventoryRecord.market.name),
                EmployeeOperateRecord.ContentItem(
                    "盘点单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MARKET_CHECK_INVENTORY_RECORD,
                        marketCheckInventoryRecord.id.toString()
                    ),
                ),
                text("中"),
            )
        )
    }

    fun updateMarketCheckInventoryRecordItemAmount(marketCheckInventoryRecordItem: MarketCheckInventoryRecordItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("调整"),
                highlightText(marketCheckInventoryRecordItem.record.market.name),
                EmployeeOperateRecord.ContentItem(
                    "盘点单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MARKET_CHECK_INVENTORY_RECORD,
                        marketCheckInventoryRecordItem.record.id.toString()
                    ),
                ),
                text("中商品"),
                highlightText(marketCheckInventoryRecordItem.goods.name),
                text("数量为"),
                highlightText(marketCheckInventoryRecordItem.amount.toString()),
            )
        )
    }

    fun submitMarketCheckInventoryRecord(marketCheckInventoryRecord: MarketCheckInventoryRecord) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("提交了"),
                highlightText(marketCheckInventoryRecord.market.name),
                EmployeeOperateRecord.ContentItem(
                    "盘点单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MARKET_CHECK_INVENTORY_RECORD,
                        marketCheckInventoryRecord.id.toString()
                    ),
                ),
            )
        )
    }


    fun deleteMarketOrder(marketOrder: MarketOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("删除了"),
                EmployeeOperateRecord.ContentItem(
                    marketOrder.market.name,
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketOrder,
                        marketOrder.toString()
                    ),
                ),
                text("卖场订单"),

                )
        )
    }

    fun deleteMarketReturnOrder(marketReturnOrder: MarketReturnOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("删除了"),
                EmployeeOperateRecord.ContentItem(
                    marketReturnOrder.market.name,
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketReturnOrder,
                        marketReturnOrder.toString()
                    ),
                ),
                text("卖场退货单"),

                )
        )
    }

    fun deleteMarketReturnOrderItem(marketReturnOrderItem: MarketReturnOrderItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("从"),
                highlightText(marketReturnOrderItem.order.market.name),
                EmployeeOperateRecord.ContentItem(
                    "卖场退货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketReturnOrder,
                        marketReturnOrderItem.order.id.toString()
                    ),
                ),
                text("中删除了"),
                highlightText(marketReturnOrderItem.goods.name),
            )
        )
    }

    fun validatedMarketReturnOrder(marketReturnOrder: MarketReturnOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("审核了"),
                EmployeeOperateRecord.ContentItem(
                    marketReturnOrder.market.name,
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketReturnOrder,
                        marketReturnOrder.toString()
                    ),
                ),
                text("卖场退货单"),

                )
        )
    }

    fun submitMarketReturnOrder(marketReturnOrder: MarketReturnOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("提交了"),
                EmployeeOperateRecord.ContentItem(
                    marketReturnOrder.market.name,
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketReturnOrder,
                        marketReturnOrder.toString()
                    ),
                ),
                text("卖场退货单"),

                )
        )
    }

    fun addMarketReturnOrderItem(marketReturnOrder: MarketReturnOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("添加了"),
                EmployeeOperateRecord.ContentItem(
                    marketReturnOrder.market.name,
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketReturnOrder,
                        marketReturnOrder.toString()
                    ),
                ),
                text("卖场退货单"),

                )
        )
    }

    fun createMarketReturnOrder(marketReturnOrder: MarketReturnOrder) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("创建了"),
                EmployeeOperateRecord.ContentItem(
                    marketReturnOrder.market.name,
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketReturnOrder,
                        marketReturnOrder.toString()
                    ),
                ),
                text("卖场退货单"),

                )
        )
    }

    fun updateMarketReturnOrderItemAmount(marketReturnOrderItem: MarketReturnOrderItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("修改了"),
                EmployeeOperateRecord.ContentItem(
                    marketReturnOrderItem.order.market.name,
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketReturnOrder,
                        marketReturnOrderItem.order.toString()
                    ),
                ),
                text("卖场退货单"),
                text("商品"),
                highlightText(marketReturnOrderItem.goods.name),
                text("数量为"),
                highlightText(marketReturnOrderItem.amount.toString()),
            )
        )
    }

    fun createMarketSaleDocument(marketSaleDocument: MarketSaleDocument) {

        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("创建了"),
                EmployeeOperateRecord.ContentItem(
                    marketSaleDocument.market.name + marketSaleDocument.account.name,
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketSaleDocument,
                        marketSaleDocument.toString()
                    ),
                ),
                text("卖场销售单"),

                )
        )
    }

    fun addMarketSaleDocumentItem(marketSaleDocument: MarketSaleDocument) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("添加了"),
                highlightText(marketSaleDocument.goodsList.last().goods.name),
                text("到"),
                highlightText(marketSaleDocument.market.name),
                EmployeeOperateRecord.ContentItem(
                    "出货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketSaleDocument,
                        marketSaleDocument.id.toString()
                    ),
                ),
                text("中"),
            )
        )
    }

    fun updateMarketSaleDocumentItemAmount(marketSaleDocumentItem: MarketSaleDocumentItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("修改了"),
                EmployeeOperateRecord.ContentItem(
                    marketSaleDocumentItem.document.market.name,
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketSaleDocument,
                        marketSaleDocumentItem.document.toString()
                    ),
                ),
                text("卖场销售单"),
                text("商品"),
                highlightText(marketSaleDocumentItem.goods.name),
                text("数量为"),
                highlightText(marketSaleDocumentItem.amount.toString()),
            )
        )
    }

    fun submitMarketSaleDocument(marketSaleDocument: MarketSaleDocument) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("提交了"),
                EmployeeOperateRecord.ContentItem(
                    marketSaleDocument.market.name,
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketSaleDocument,
                        marketSaleDocument.toString()
                    ),
                ),
                text("卖场销售单"),

                )
        )
    }

    fun auditMarketSaleDocument(marketSaleDocument: MarketSaleDocument) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("审核通过了"),
                EmployeeOperateRecord.ContentItem(
                    marketSaleDocument.market.name,
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketSaleDocument,
                        marketSaleDocument.toString()
                    ),
                ),
                text("卖场销售单"),

                )
        )
    }

    fun deleteMarketSaleDocument(marketSaleDocument: MarketSaleDocument) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("删除了"),
                EmployeeOperateRecord.ContentItem(
                    marketSaleDocument.market.name,
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketSaleDocument,
                        marketSaleDocument.toString()
                    ),
                ),
                text("卖场销售单"),

                )
        )
    }

    fun deleteDirectSaleOrderItem(item: DirectSaleOrderItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("从"),
                highlightText(item.order.client.name),
                EmployeeOperateRecord.ContentItem(
                    "销售单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.DirectSaleOrder,
                        item.order.id.toString()
                    ),
                ),
                text("中删除了"),
                highlightText(item.goods.name),
            )
        )

    }

    fun deleteMarketOrderItem(item: MarketOrderItem) {

        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                mutableListOf(
                    employeeName(),
                    text("从"),
                    highlightText(item.marketOrder.market.name),
                    EmployeeOperateRecord.ContentItem(
                        "卖场订单单",
                        resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                            EmployeeOperateRecord.ContentItem.ResourceType.MarketOrder,
                            item.marketOrder.id.toString()
                        ),
                    ),
                    text("中删除了"),
                    highlightText(item.goods.name),
                ).apply {
                    if (item.subGoods == null) null else {
                        this.add(text("的子商品"))
                        this.add(highlightText(item.subGoods!!.name))
                    }
                }
            )
        )
    }

    fun deleteMarketSaleDocumentItem(item: MarketSaleDocumentItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("从"),
                highlightText(item.document.market.name),
                EmployeeOperateRecord.ContentItem(
                    "卖场销售单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketSaleDocument,
                        item.document.id.toString()
                    ),
                ),
                text("中删除了"),
                highlightText(item.goods.name),
            )
        )
    }

    fun deleteProcurementItem(item: ProcurementItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("从"),
                highlightText(item.procurement.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购退货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.Procurement,
                        item.procurement.id.toString()
                    ),
                ),
                text("中删除了"),
                highlightText(item.subGoods?.name?:item.goods.name),
            )
        )
    }

    fun updateMarketOrderItemNote(item: MarketOrderItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("备注了"),
                highlightText(item.marketOrder.market.name),
                EmployeeOperateRecord.ContentItem(
                    "订单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.MarketOrder,
                        item.marketOrder.id.toString()
                    ),
                ),
                text("中商品"),
                highlightText(item.subGoods?.name ?: item.goods.name),
                text("："),
                highlightText(item.note?:"<空>"),
            )
        )
    }

    fun createProcurementReturn(`return`: ProcurementReturn) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("创建了"),
                highlightText(`return`.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购退货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.PROCUREMENT_RETURN,
                        `return`.id.toString()
                    ),
                ),
            )
        )
    }

    fun addProcurementReturnItem(`return`: ProcurementReturn) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("添加了"),
                highlightText(`return`.items.last().goods.name),
                text("到"),
                highlightText(`return`.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购退货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.PROCUREMENT_RETURN,
                        `return`.id.toString()
                    ),
                ),
                text("中"),
            )
        )
    }

    fun deleteProcurementReturnItem(item: ProcurementReturnItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("从"),
                highlightText(item.procurementReturn.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购退货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.PROCUREMENT_RECEIPT,
                        item.procurementReturn.id.toString()
                    ),
                ),
                text("中"),
                text("删除了"),
                highlightText(item.goods.name),
            )
        )
    }

    fun updateProcurementReturnItemAmount(item: ProcurementReturnItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("调整"),
                highlightText(item.procurementReturn.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购退货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.Procurement,
                        item.procurementReturn.id.toString()
                    ),
                ),
                text("中商品"),
                highlightText(item.subGoods?.name?:item.goods.name),
                text("数量为"),
                highlightText(item.amount.toString() ),
            )
        )
    }

    @Transactional
    fun updateProcurementReturnItemNote(item: ProcurementReturnItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("备注了"),
                highlightText(item.procurementReturn.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购退货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.PROCUREMENT_RETURN,
                        item.procurementReturn.id.toString()
                    ),
                ),
                text("中商品"),
                highlightText(item.subGoods?.name ?: item.goods.name),
                text("："),
                highlightText(item.note?:"<空>"),
            )
        )
    }

    @Transactional
    fun deleteProcurementReceipt(procurementReceipt: ProcurementReceipt) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("删除了"),
                EmployeeOperateRecord.ContentItem(
                    procurementReceipt.supplier.name,
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.PROCUREMENT_RECEIPT,
                        procurementReceipt.id.toString(),
                    ),
                ),
                text("采购入库单"),

                )
        )
    }

    fun updateProcurementReturnItemPrice(procurementReturnItem: ProcurementReturnItem) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("调整"),
                highlightText(procurementReturnItem.procurementReturn.supplier.name),
                EmployeeOperateRecord.ContentItem(
                    "采购退货单",
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.Procurement,
                        procurementReturnItem.procurementReturn.id.toString()
                    ),
                ),
                text("中商品"),
                highlightText(procurementReturnItem.subGoods?.name?:procurementReturnItem.goods.name),
                text("退货价为"),
                highlightText(procurementReturnItem.price.toString()),
            )
        )
    }

    fun clearProcurementReceipt(procurementReceipt: ProcurementReceipt) {
        employeeOperateRecordRepository.save(
            EmployeeOperateRecord.withContent(
                employeeName(),
                text("清空了"),
                EmployeeOperateRecord.ContentItem(
                    procurementReceipt.supplier.name,
                    resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                        EmployeeOperateRecord.ContentItem.ResourceType.PROCUREMENT_RECEIPT,
                        procurementReceipt.id.toString(),
                    ),
                ),
                text("采购入库单"),
                )
        )

    }

    @Transactional
    fun configureMarketShoppingGuideSalaryPlan(plan: MarketShoppingGuideSalaryPlan) {
        employeeOperateRecordRepository.save(EmployeeOperateRecord.withContent(
            employeeName(),
            text("配置了"),
            EmployeeOperateRecord.ContentItem(
                plan.market.name,
                resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                    EmployeeOperateRecord.ContentItem.ResourceType.MARKET,
                    plan.market.id.toString(),
                ),
            ),
            text("工资方案"),
        ))
    }

    @Transactional
    fun configureMarketSettlementPlan(plan: MarketSettlementPlan){
        employeeOperateRecordRepository.save(EmployeeOperateRecord.withContent(
            employeeName(),
            text("配置了"),
            EmployeeOperateRecord.ContentItem(
                plan.market.name,
                resource = EmployeeOperateRecord.ContentItem.ResourceTypeWithId(
                    EmployeeOperateRecord.ContentItem.ResourceType.MARKET,
                    plan.market.id.toString(),
                ),
            ),
            text("结算方案"),
        ))
    }


}